<template>
  <div class="uploadimg">
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :before-upload="beforeAvatarUpload"
      :http-request="httpRequest"
    >
      <img
        v-if="value"
        v-imgerror="errorImg"
        :src="value"
        class="avatar"
        @click.stop="isShow = true"
      >
      <i v-else class="el-icon-plus avatar-uploader-icon" />
    </el-upload>
    <!-- 关闭按钮 -->
    <div v-if="value" class="close" @click="del">X</div>
    <!-- 查看大图 -->
    <el-dialog :visible.sync="isShow" title="大图预览" width="400px">
      <img v-imgerror="errorImg" style="width: 100%" :src="value" alt="">
    </el-dialog>
    <!-- 进度条 -->
    <transition name="fade">
      <el-progress
        v-if="showProgress"
        :percentage="percentage"
        style="width: 178px"
      />
    </transition>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDvf9MIxLSIr9AuNZU8WdT2KL5XicFbgTz',
  SecretKey: 'GFxPbW9VbRzGGzSeaFe17xWooY1EBvQf'
})
export default {
  name: 'UpLoadImg',
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      errorImg: require('@/assets/common/head.jpg'),
      isShow: false,
      percentage: 0,
      showProgress: false
    }
  },
  methods: {
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    //  自定义上传
    httpRequest(data) {
      console.log('data', data)
      this.showProgress = true
      this.percentage = 0
      cos.putObject(
        {
          Bucket: 'hradmin-1308138780' /* 必须 */,
          Region: 'ap-guangzhou' /* 存储桶所在地域，必须字段 */,
          Key: data.file.name /* 必须 */,
          StorageClass: 'STANDARD',
          Body: data.file, // 上传文件对象
          onProgress: (progressData) => {
            this.percentage = +(progressData.percent * 100).toFixed(2)
            setTimeout(() => {
              this.showProgress = false
            }, 2000)
            console.log(JSON.stringify(progressData))
          }
        },
        (err, data) => {
          if (!err) {
            this.$emit('input', 'http://' + data.Location)
          }
          console.log(err || data)
        }
      )
    },
    del() {
      this.$emit('input', '')
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
::v-deep .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.upload {
  width: 178px;
  height: 190px;
  position: relative;
  &:hover {
    .close {
      display: block;
    }
  }
  .close {
    position: absolute;
    top: 5px;
    right: 10px;
    color: aqua;
    display: none;
    cursor: pointer;
  }
}
.fade-enter-active,
.fade-leave-active {
  transition: all 05s;
}
.fade-enter,
.fade-leave-to {
  opacity: 0;
}
</style>
